package com.yd;

import com.yd.bean.DocNodeBean;
import com.yd.config.Config;
import com.yd.util.FileUtils;
import com.yd.util.XmlUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 对字符串文件重新排序
 * 参考的排序和需要排序的xml文件放在resource/order 目录下面，重新拍好序的放在{@link Config#OrderFileDir} 目录下
 */
public class OrderStringClient {
    public static void main(String[] args) {
        File inputFile = Config.orderSourceFileDir;
        File outFile = Config.OrderFileDir;
        orderDir(inputFile, outFile);
    }

    public static void orderDir(File inputDir, File outputDir) {
        if (inputDir.exists()&&inputDir.isDirectory()) {
            List<File> orderFiles = new ArrayList<>();
            for (File file : inputDir.listFiles()) {
                if (!file.getName().contains(Config.needOrderSuffix)) {
                    //选项需要排序的文件
                    orderFiles.add(file);
                }
            }

            for (File file : orderFiles) {
                System.out.println("orderDir>>>遍历文件 >>>>path = " + file.getPath());
                String name = FileUtils.getFileNameNoExtension(file);
                String fileExtension = FileUtils.getFileExtension(file);
                File needOrderFile = new File(inputDir, name + Config.needOrderSuffix + "." + fileExtension);
                System.out.println("orderDir>>>遍历文件 >>待排序的文件>>needOrderFile = " + needOrderFile.getPath() + "-----exist=" + needOrderFile.exists());
                if (needOrderFile.isFile() && needOrderFile.exists()) {
                    orderFile(file, needOrderFile, new File(outputDir,
                            name + Config.needOrderFinishSuffix + "." + fileExtension));
                }
            }
        }else {
            System.out.println("orderDir>>>遍历文件 >>>>inputDir = " + inputDir.getPath() +"====不存在，编译一下项目Build->Build Project");
        }
    }

    public static void orderFile(File inputFile, File needOrderFile, File orderOutputFile) {
        System.out.println("orderFile>>>排序文件 >>>inputFile =" + inputFile.getPath());
        System.out.println("orderFile>>>排序文件 >>>needOrderFile =" + needOrderFile.getPath());
        DocNodeBean docNodeBean = XmlUtil.parseSourceDoc(inputFile.getPath());
        DocNodeBean orderDocNodeBean = XmlUtil.parseSourceDoc(needOrderFile.getPath());
        XmlUtil.orderDoc(docNodeBean, orderDocNodeBean, orderOutputFile);
    }
}


